package com.erp.myAdapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import com.erp.R
import com.erp.bean.WtPhotoBean
import com.erp.util.Myutil
import com.erp.util.Sputil
import kotlinx.android.synthetic.main.vp_iv_photo_item.view.*
import java.util.*

/**
 * Created by Administrator on 2018/6/5/005.
 * 作者：刘洋
 */
class WenTiTKAdapter(val context : Context,val channelList: MutableList<WtPhotoBean>,var code : Int) : BaseAdapter() {
    var minflater: LayoutInflater = LayoutInflater.from(context)
    var listcb: MutableList<Int> = ArrayList()
    /** 要删除的position  */
    override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
        var v :View=minflater.inflate(R.layout.vp_iv_photo_item,null)

        val mLayoutParams = v.edit_ll.layoutParams
        mLayoutParams.width = Sputil.sp("erp").getInt("width", 0) / 3
        mLayoutParams.height = Sputil.sp("erp").getInt("width", 0) / 3
        v.edit_ll.layoutParams = mLayoutParams
        if (position < channelList.size) {
            val wtPhotoBean = channelList[position]
            if(code == 4){
                v.LlGuiBao.visibility=View.VISIBLE
                Myutil.settv(wtPhotoBean.c_name, v.tv_GuiMing)
                Myutil.settv(wtPhotoBean.cnum, v.tv_GuiHao)
            }
            Myutil.showhttpphoto(wtPhotoBean.uri, v.icon_iv, context)
            v.iv_delet.visibility = View.GONE
            for (i in listcb.indices) {
                if (listcb[i] == position) {
                    v.iv_delet.visibility = View.VISIBLE
                    break
                }
            }
        }else{
            v.iv_delet.visibility = View.GONE
            v.icon_iv.setImageResource(R.mipmap.erp_photo)
        }
        return v
    }

    override fun getItem(position: Int): Any {
        return channelList[position]
    }

    override fun getItemId(position: Int): Long {
        return position.toLong()
    }

    override fun getCount(): Int {
        return channelList.size+1
    }
    class ViewHolder(var v : View){
        val llll = v.edit_ll
        val iv_icon= v.icon_iv
        val iv_delete =v.iv_delet
        val tv_GuiHao =v.tv_GuiHao
        val tv_GuiMing =v.tv_GuiMing
        val LlGuiBao = v.LlGuiBao
    }
    fun downdelete(position: Int){
        if(position < channelList.size) {
            if (listcb.contains(position)) {
                listcb.remove(position)
            } else {
                listcb.add(position)
            }
        }
    }
    fun clean(){
        listcb.clear()
    }
}